package com.qf.petmall.starter.login.controller;

import com.qf.petmall.starter.login.entity.Users;
import com.qf.petmall.starter.login.service.IUsersService;
import com.qf.petmall.starter.utils.R;


import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import sun.security.util.Debug;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.UUID;

/**
 * @author zxz
 * @description TODO
 * @date 2022/12/9 17:06
 */
@RestController
@CrossOrigin
@RequestMapping("/users")
public class UserImgController {
    @Autowired
    IUsersService usersService;
    @PostMapping("/upload")
    public R uploadUserImg(MultipartFile userImg,Integer userId) throws IOException {
        return usersService.upload(userImg,userId);
    }

    @GetMapping("/getimg/{filename}")
    public R getimg(@PathVariable("filename") String filename, HttpServletResponse response){

        String dir = System.getProperty("user.dir");
        String filePath = dir + File.separator + "imgs"+File.separator;
        String finalFilePath = filePath + filename;
        String[] split = filename.split("\\.");
        try {
            response.setContentType("image/jpeg");
       /*     response.addHeader("Content-Disposition","attachment;filename="+filename);*/
            IOUtils.copy(new FileInputStream(finalFilePath),response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
            return R.error("下载失败");
        }

        return R.ok("下载成功");
    }


    }
